home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / xlib50.zip / FLAT.ASM < prev    next >
Assembly Source File  |  1995-02-15  |  9KB  |  176 lines

  1. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. ;                 Flat-Model Programming Using FLAT.INC
  3. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  4. ;   This program illustrates usage of FLAT.INC to perform flat-model
  5. ;protected-mode programming in DOS.  The program performs a simple hex
  6. ;dump of memory from any requested address.  At the input prompt, type
  7. ;the starting hex address to be viewed and hit ENTER.  Alternatively,
  8. ;hit DOWN/UP ARROW or PAGE UP/DOWN to scroll.  Hit ESC to exit.
  9. ;   WARNING:  This program may crash with a page fault (exception 14)
  10. ;if page protection is being enforced by a memory manager or operating
  11. ;system.  Run the program under a clean configuration or HIMEM.SYS to
  12. ;get an unrestricted view of memory.
  13. ;   The key to this program is the include file FLAT.INC.  FLAT.INC is
  14. ;highly documented.  It is suggested that it be thoroughly read.  This
  15. ;program also makes considerable use of PMIO.INC for input/output.  This
  16. ;file also has extensive documentation.
  17. ;   The following template reveals the simplicity of flat-model programs
  18. ;using FLAT.INC:
  19. ;
  20. ;              INCLUDE        FLAT.INC       ;Always first line in program
  21. ;
  22. ;MAIN          PROC NEAR                     ;Execution always begins at MAIN
  23. ;              .
  24. ;              .
  25. ;              RET                           ;Execution ends here
  26. ;MAIN          ENDP
  27. ;
  28. ;              [Other Procedures]            ;All procedures must be near
  29. ;
  30. ;              [Program Data]
  31. ;
  32. ;              END
  33. ;
  34. ;   The order of MAIN, other procedures, and data is not important.  The
  35. ;important things are that FLAT.INC be the first statement, that execution
  36. ;begin at MAIN, and that MAIN and all other procedures be near.
  37. ;   FLAT.INC enables a close approximation of the flat model.  In the true
  38. ;flat model all segments have base address at zero and limits equal to
  39. ;4Gb.  The flat model is therefore unsegmented.  One of the principle
  40. ;attractions of the model is that segment registers never have to be
  41. ;reloaded.  The XLIB flat model is like the true flat model in all regards
  42. ;except that segments are not based at zero.  Unfortunately, limitations
  43. ;to DOS disallow elimination of this difference.  However, the difference
  44. ;has surprisingly little affect upon code.  In fact, the difference
  45. ;becomes relevant only when the program must access external data.  The
  46. ;addresses to such data must be normalized by subtracting the segment
  47. ;base address from their offsets.  For example, the color screen would
  48. ;be accessed in the flat model at offset B8000H.  In the approximate
  49. ;model, the offset must be B8000H - BASEADDRESS, where BASEADDRESS is
  50. ;the linear address of the segment base.  This methodology is used in
  51. ;the program below.
  52. ;   NOTICE:  TASM programmers should remove the semicolon on the line
  53. ;defining TASMMODE.
  54.  
  55. TRUE           EQU            1
  56. FALSE          EQU            0
  57.  
  58. ;TASMMODE      EQU            TRUE           ;Remove comment for TASM
  59.                INCLUDE        FLAT.INC
  60.  
  61. ;ASCII codes and scan codes
  62. CR             EQU            13
  63. UP             EQU            48H
  64. DOWN           EQU            50H
  65. PAGEDOWN       EQU            51H
  66. PAGEUP         EQU            49H
  67.  
  68. ;   Upon entry to MAIN, DS:EBX will point to the program segment prefix, and
  69. ;EDX will equal the linear base address of CS, DS, and SS.  ES will be a
  70. ;true flat-model segment (base = 0).  All of these segments will have 4Gb
  71. ;limits.  FS will contain a selector to segment DSEG which is the XLIB data
  72. ;segment.  See XLIB documentaion for information about public symbols in
  73. ;DSEG.  GS will contain a selector to DGROUP.  Both FS and GS will have 64K
  74. ;limits.
  75. ;   FLAT.INC can be configured to change the segment settings above.  See
  76. ;comments in FLAT.INC for details.
  77.  
  78. ;Perform hex dump from anywhere in memory.  Start at address 00000000H and
  79. ;let user key in addresses thereafter.
  80. MAIN           PROC NEAR
  81.                XOR            ESI,ESI        ;ESI will be the linear address of displayed memory
  82.                MOV            EDI,ESI
  83.                SUB            EDI,EDX        ;EDI will be the normalized address of displayed memory
  84. MAINLOOP:      PUSH           ESI            ;Save addresses
  85.                PUSH           EDI
  86.                CALL           CLS            ;Clear screen
  87.                MOV            ECX,368        ;Print 368 bytes, 16 per line, 23 lines
  88. LINELOOP:      MOV            EAX,ESI        ;Print linear address of hex dump for each line
  89.                CALL           PHD            ;PHD = "Print Hexadecimal DWORD" in EAX
  90.                MOV            AL,":"
  91.                CALL           PCH            ;PCH = "Print Character" in AL
  92.                MOV            AL,4
  93.                CALL           SPC            ;SPC prints AL spaces
  94. BYTELOOP:      MOV            AL,[EDI]       ;Get and display memory
  95.                CALL           PHB            ;PHB = "Print Hexadecimal Byte" in AL
  96.                MOV            AL,2           ;Print 2 spaces
  97.                CALL           SPC
  98.                INC            ESI            ;Advance to next byte
  99.                INC            EDI
  100.                DEC            ECX            ;Decrement byte counter
  101.                TEST           ECX,0FH        ;See if complete line (16 bytes) yet printed
  102.                JNZ            BYTELOOP
  103.                CALL           CRLF           ;Do carriage return and linefeed to next line
  104.                OR             ECX,ECX        ;See if all 23 lines printed
  105.                JNZ            LINELOOP
  106.                POP            EDI            ;POP normalized address
  107.                POP            ESI            ;POP linear address
  108.                MOV            EBX,OFFSET PROMPT             ;Display input prompt at bottom line
  109.                CALL           PSTR           ;PSTR = "Print String" at DS:EBX
  110.                MOV            HEXADDRESS,0   ;Make initial string null
  111.                MOV            EBX,OFFSET HEXADDRESS
  112.                MOV            EAX,8          ;Limit input field to 8 characters
  113.                CALL           EDITSTR        ;Input new address.  Returned AX defines last typed character.  See EDITSCRN in PMIO.INC
  114.                OR             AH,AH          ;See if a nonASCII key was pressed last
  115.                JS             CHECKDOWN
  116.                CMP            AL,CR          ;See if CR was last character typed
  117.                JNE            EXIT
  118.                CALL           CONVERTHEX     ;Convert hex string at DS:EBX to numeric in EAX
  119.                AND            AL,0F0H        ;Set requested address to mod 16 boundary
  120.                MOV            ESI,EAX        ;Update linear address in ESI
  121.                MOV            EDI,EAX        ;Compute new normalized address in EDI
  122.                SUB            EDI,EDX
  123.                JMP            MAINLOOP
  124. CHECKDOWN:     CMP            AL,DOWN        ;See if DOWN ARROW was pressed
  125.                JNE            CHECKUP
  126.                ADD            EDI,16         ;Move screen down one line (16 bytes)
  127.                ADD            ESI,16
  128.                JMP            MAINLOOP
  129. CHECKUP:       CMP            AL,UP          ;See if UP ARROW was pressed
  130.                JNE            CHECKPAGEDOWN
  131.                SUB            EDI,16         ;Move screen up one line
  132.                SUB            ESI,16
  133.                JAE            MAINLOOP
  134. MEMBOTTOM:     XOR            ESI,ESI        ;Are at bottom of memory
  135.                XOR            EDI,EDI
  136.                SUB            EDI,EDX
  137.                JMP            MAINLOOP
  138. CHECKPAGEDOWN: CMP            AL,PAGEDOWN    ;See if PAGE DOWN was pressed
  139.                JNE            CHECKPAGEUP
  140.                ADD            ESI,368        ;Move down one page
  141.                ADD            EDI,368
  142.                JMP            MAINLOOP
  143. CHECKPAGEUP:   CMP            AL,PAGEUP      ;See if PAGE UP was pressed
  144.                JNE            EXIT
  145.                SUB            EDI,368        ;Move up one page
  146.                SUB            ESI,368
  147.                JAE            MAINLOOP
  148.                JMP            MEMBOTTOM
  149. EXIT:          RET
  150. MAIN           ENDP
  151.  
  152. PROMPT         DB "Input Hex Address: ",0    ;Input prompt
  153. HEXADDRESS     DB 9 DUP(0)                   ;String for 8 hex digits plus zero termination character
  154.  
  155. ;Convert hex string at DS:EBX to numeric in EAX.  Does not check syntax.
  156. CONVERTHEX     PROC NEAR
  157.                PUSH           EBX
  158.                PUSH           EDX
  159.                XOR            EAX,EAX        ;Accumulate value in EAX
  160.                XOR            EDX,EDX
  161. CHARLOOP:      MOV            DL,[EBX]       ;Get ASCII hex digit
  162.                SUB            DL,48          ;Convert to ASCII digit to numeric
  163.                JB             EXIT           ;String is terminated with 0
  164.                SHL            EAX,4          ;Multiply cumulative by 16
  165.                ADD            EAX,EDX        ;Accumulate last digit
  166.                INC            EBX            ;Advance to next digit
  167.                JMP            CHARLOOP
  168. EXIT:          POP            EDX
  169.                POP            EBX
  170.                RET
  171. CONVERTHEX     ENDP
  172.  
  173.                INCLUDE        PMIO.INC       ;Protected-mode input/output
  174.  
  175.                END
  176.